﻿using System.Collections.Generic;
using JetBrains.ReSharper.Daemon.Stages;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.Psi.CSharp.Tree;
using Twins.Daemon;
using Twins.Interface;
using System.Linq;
namespace Twins.Classes
{
    public class DispatherProject
    {
        private readonly ILookCSharpFile lookCSharpFile;
        private readonly ITwinsHighLighting twinsHighLighting;
        private readonly ISelectorDuplicationHashsLineCodeFiles selectorDuplicationHashsLineCodeFiles;
        private readonly List<IHashsFile> hashsFiles = new List<IHashsFile>();
        private IHashsFile flowingFile;

        public DispatherProject(ILookCSharpFile lookCSharpFile, ITwinsHighLighting twinsHighLighting,ISelectorDuplicationHashsLineCodeFiles selectorDuplicationHashsLineCodeFiles)
        {
            this.lookCSharpFile = lookCSharpFile;
            this.twinsHighLighting = twinsHighLighting;
            this.selectorDuplicationHashsLineCodeFiles = selectorDuplicationHashsLineCodeFiles;
        }

        public void LookCSharpFile(IHighlightingConsumer highlightingConsumer, ICSharpFile cSharpFile)
        {
            flowingFile.HashsLineCodeMethods  = lookCSharpFile.StartLook(highlightingConsumer,cSharpFile);
            var duplicationHashsLineCode = selectorDuplicationHashsLineCodeFiles.SearchDuplication(hashsFiles);
            twinsHighLighting.UnderlineDuplication(duplicationHashsLineCode, highlightingConsumer);
        }

        public void CreateFile(IPsiSourceFile sourceFile)
        {
            flowingFile = null;
            foreach (var file in hashsFiles.Where(file => !file.SourceFile.IsValid()))
                hashsFiles.Remove(file);
            foreach (var file in hashsFiles.Where(file => file.SourceFile== sourceFile))
                flowingFile = file;
            if (flowingFile != null) return;
            flowingFile = new HashsFile(sourceFile);
            hashsFiles.Add(flowingFile);
        }
    }
}